// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../../BASE/ComponentBase";
const {ccclass, property} = cc._decorator;

@ccclass
export default class JJ extends ComponentBase {
    @property
    private _strength: number = 10; // 抖动强度

    shake(duration: number, strength: number) {
        let cameraNode: cc.Node = this.node; // 假设你的摄像头节点是当前组件所在的节点
        let originalPos = cameraNode.position; // 保存原始位置

        let shakeAction = cc.sequence(
            cc.callFunc(() => {
                cameraNode.setPosition(originalPos); // 重置位置
            }),
            cc.repeatForever(
                cc.sequence(
                    cc.moveBy(duration / 4, cc.v2(strength, 0)),
                    cc.moveBy(duration / 4, cc.v2(-strength, 0)),
                    cc.moveBy(duration / 4, cc.v2(0, strength)),
                    cc.moveBy(duration / 4, cc.v2(0, -strength))
                )
            )
        );

        cameraNode.runAction(shakeAction);
    }









   

    @property
    private zoomDuration: number =  0.2; // 放大持续时间

    @property
    private zoomScale: number =1.2; // 放大的比例

    @property
    private zoomBackDuration: number = 0.2; // 回缩持续时间

    @property
    private delay: number =  0.2; // 放大后延迟回缩的时间
ok:boolean=true
    start() {
        // 确保目标节点已经被设置
    
    }


    JJ(targetNode: cc.Node,k:number) {
        if (!targetNode) {
            cc.error('Target node is not set!');
            return;
        }

        // 获取Main Camera
        const mainCamera: cc.Camera = this.node.getComponent(cc.Camera);

        // 定义一个变量来存储目标位置
        let targetPosition = this.node.parent.convertToNodeSpaceAR(this.getWorldPos(targetNode));

        // 定义一个函数来计算偏移量
        function calculateOffset(distance: number, maxDistance: number, maxOffset: number): cc.Vec3 {
            // 计算偏移比例，距离越远，比例越小
            let offsetRatio = Math.max(0, 1 - distance / maxDistance);
            // 计算实际偏移量
            let offset = targetPosition.sub(mainCamera.node.position).normalize().mul(maxOffset * offsetRatio);
            return offset;
        }

        // 设置一个循环来更新摄像头位置
        let moveCameraToTarget = () => {
            // 计算目标节点与摄像头之间的距离
            let distance = mainCamera.node.position.sub(targetPosition).mag();
            // 设置最大距离，超过这个距离偏移量将不再增加
            let maxDistance =6000*k;
            // 设置最大偏移量
            let maxOffset =70*k;

            // 计算偏移量
            let offset = calculateOffset(distance, maxDistance, maxOffset);

            // 更新摄像头位置
            mainCamera.node.setPosition(mainCamera.node.position.add(offset));

            // 如果距离目标位置还有一定距离，继续移动
            if (mainCamera.node.position.sub(targetPosition).mag() > 1&&!this.ok) {
                requestAnimationFrame(moveCameraToTarget);
                console.log(this.ok)
            }
        };

        // 开始移动摄像头
        this.ok=false
        moveCameraToTarget();

        // 放大
        cc.tween(mainCamera)
            .to(this.zoomDuration, { zoomRatio: this.zoomScale })
            .call(() => {
               // 放大后延迟一段时间开始回缩
              setTimeout(() => {
                  cc.tween(mainCamera)
                   .to(this.zoomBackDuration*k, { zoomRatio: 1 }
                        
                 )
                   .start();
              }, this.delay);
                this.ok=true
            })
            .start();
    }
HG(targetNode:cc.Node){

    if (!targetNode) {
        cc.error('Target node is not set!');
        return;
    }

    // 获取Main Camera
    const mainCamera: cc.Camera = this.node.getComponent(cc.Camera)

    // 聚焦到目标节点
    cc.tween(mainCamera.node)
        .to(0.85, { position:cc.v3(20,-40)})
        .start()
       
 

}






}


    
